package com.order.controller.buyer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.annotation.LoggerManage;
import com.order.pojo.ProductCategory;
import com.order.pojo.ProductInfo;
import com.order.service.ProductCategoryService;
import com.order.service.ProductInfoService;
import com.order.dto.ProductInfoVo;
import com.order.vo.ProductVo;
import com.order.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "餐品展示接口")
@RestController
@Slf4j//输出语句
@ApiResponses({
        @ApiResponse(code = 200, message = "新增成功", response = ProductCategory.class),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 403, message = "权限被禁止"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
@RequestMapping("sell/buyer")
public class BuyerProductinfoController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    RedisTemplate redisTemplate;
    @LoggerManage(value = "查询餐品列表")
    @GetMapping("product/list")
    public ResultVo<List<ProductVo>> getProductinfo(){
        List<ProductVo> cacheKey=(List<ProductVo>)redisTemplate.opsForValue().get("cacheKey");
        if(cacheKey==null){

            //获取全部类别集合
            List<ProductCategory> productCategoryList=productCategoryService.list(new QueryWrapper<ProductCategory>().eq("del",0));
            //name+type+foods集合
            List<ProductVo> productVoList=new ArrayList<>();
            for(ProductCategory pl:productCategoryList){
                //一个类别 //foods
                List<ProductInfoVo> list=new ArrayList<>();
                List<ProductInfo> productInfoList=productInfoService.list(new QueryWrapper<ProductInfo>().eq("category_type",pl.getCategoryId()));
                ProductVo productVo=new ProductVo();
                productVo.setCategoryName(pl.getCategoryName());
                productVo.setCategoryType(pl.getCategoryType());//4,5,7,8
                for(ProductInfo ls:productInfoList){

                    ProductInfoVo productInfoVo=new ProductInfoVo();
                    productInfoVo.setProductId(ls.getProductId());
                    productInfoVo.setProductName(ls.getProductName());
                    productInfoVo.setProductPrice(ls.getProductPrice());
                    productInfoVo.setProductIcon(ls.getProductIcon());
                    productInfoVo.setProductDescription(ls.getProductDescription());
                    list.add(productInfoVo);
                }
                productVo.setProductInfoVOList(list);

                productVoList.add(productVo);
            }
            log.info("没有缓存，查数据库！");
            redisTemplate.opsForValue().set("cacheKey",productVoList,20, TimeUnit.SECONDS);
            return new ResultVo<>(0,"查询成功",productVoList);
        }
        log.info("有缓存，走缓存！");
        return new ResultVo<>(0,"查询成功",( List<ProductVo>)redisTemplate.opsForValue().get("cacheKey"));
    }
}